

package com.xy.controller.msg;

import com.xy.common.base.ApiBaseResponse;
import com.xy.common.base.BaseController;
import com.xy.entity.Coupon.Coupon;
import com.xy.entity.User;
import com.xy.entity.msg.*;
import com.xy.entity.order.OrderMain;
import com.xy.entity.product.Picture;
import com.xy.repository.CouponRespository;
import com.xy.repository.PictureRespository;
import com.xy.repository.UserRepository;
import com.xy.repository.msg.ActivityMsgRepository;
import com.xy.repository.msg.OrderMsgRepository;
import com.xy.repository.msg.ProductMsgRepository;
import com.xy.repository.msg.SysMsgRepository;
import com.xy.repository.order.OrderMainRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Api(value = "我的消息Controller", description = "我的消息接口")
@RestController
@RequestMapping(value = "api/usermsg")
public class SysMsgController extends BaseController {


    private UserRepository userRepository;
    private OrderMainRepository orderMainRepopsitory;
    private CouponRespository couponRespository;
    private SysMsgRepository sysMsgRepository;
    private ActivityMsgRepository activityMsgRepository;
    private OrderMsgRepository orderMsgRepository;
    private ProductMsgRepository productMsgRepository;
    private PictureRespository pictureRespository;

    @Autowired
    public SysMsgController(
            UserRepository userRepository,
           OrderMainRepository orderMainRepopsitory,
            CouponRespository couponRespository,
            SysMsgRepository sysMsgRepository,
            ActivityMsgRepository activityMsgRepository,
            OrderMsgRepository orderMsgRepository,
            ProductMsgRepository productMsgRepository,
            PictureRespository pictureRespository
    ){
        this.userRepository = userRepository;
        this.orderMainRepopsitory = orderMainRepopsitory;
        this.couponRespository=couponRespository;
        this.sysMsgRepository=sysMsgRepository;
        this.activityMsgRepository=activityMsgRepository;
        this.orderMsgRepository=orderMsgRepository;
        this.productMsgRepository=productMsgRepository;
        this.pictureRespository=pictureRespository;
    }

    @ApiOperation(value = "返回是否有未读消息")
    @ResponseStatus(HttpStatus.OK)
    @PostMapping(value = "/msg")
    @Transactional
    public ResponseEntity getAllUserMsg() {
        initBaseInfo();
        return ResponseEntity.ok(ApiBaseResponse.fromHttpStatus(HttpStatus.OK,0)) ;
    }



    @ApiOperation(value = "显示所有系统消息")
    @ResponseStatus(HttpStatus.OK)
    @PostMapping(value = "/sysmsg")
    @Transactional
    public ResponseEntity getAllSysMsg() {
        initBaseInfo();
        User user = userRepository.findAllByUuid(userId);
        Set<SystemMsg> systemMsgs = user.getSystemMsgs();
        return ResponseEntity.ok(ApiBaseResponse.fromHttpStatus(HttpStatus.OK,systemMsgs));
    }

    @ApiOperation(value = "阅读系统消息")
    @ResponseStatus(HttpStatus.OK)
    @PostMapping(value = "/sysmsg/{uuid}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uuid", value = "系统信息uuid", required = true),
    })
    @Transactional
    public ResponseEntity oneSysMsg(@PathVariable("uuid") String uuid) {
        SystemMsg systemMsg=sysMsgRepository.findByUuid(uuid);
        if(systemMsg.getStatus()==0){
            systemMsg.setStatus(1);
        }
        return ResponseEntity.ok(ApiBaseResponse.fromHttpStatus(HttpStatus.OK,systemMsg));
    }

    @ApiOperation(value = "所有优惠活动消息")
    @ResponseStatus(HttpStatus.OK)
    @PostMapping(value = "/activitymsg")
    @Transactional
    public ResponseEntity getAllactivityMsg() {
        initBaseInfo();
        User user = userRepository.findAllByUuid(userId);
        Set<ActivityMsg> activityMsgs = user.getActivityMsgs();
        List<Picture> pictures = pictureRespository.findByRemark("活动图");
        for (ActivityMsg activityMsg:activityMsgs){
            activityMsg.setPictures(pictures);
        }
        return ResponseEntity.ok(ApiBaseResponse.fromHttpStatus(HttpStatus.OK,activityMsgs));
    }

    @ApiOperation(value = "阅读优惠活动消息")
    @ResponseStatus(HttpStatus.OK)
    @PostMapping(value = "/oneactivitymsg/{uuid}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uuid", value = "活动信息uuid", required = true),
    })
    @Transactional
    public ResponseEntity oneActivityMsg(@PathVariable("uuid") String uuid) {
        ActivityMsg activityMsg=activityMsgRepository.findByUuid(uuid);
        List<Picture> pictures = pictureRespository.findByRemark("活动图");
            activityMsg.setPictures(pictures);
        if(activityMsg.getStatus()==0){
            activityMsg.setStatus(1);
        }
        return ResponseEntity.ok(ApiBaseResponse.fromHttpStatus(HttpStatus.OK,activityMsg));
    }

    @ApiOperation(value = "所有订单消息")
    @ResponseStatus(HttpStatus.OK)
    @PostMapping(value = "/ordermsg")
    @Transactional
    public ResponseEntity getAllOrderMsg() {
        initBaseInfo();
        OrderMain orderMain = orderMainRepopsitory.findAllByUserId(userId);
        Set<OrderMsg> orderMsgs = orderMain.getOrderMsgs();
        return ResponseEntity.ok(ApiBaseResponse.fromHttpStatus(HttpStatus.OK,orderMsgs));
    }


    @ApiOperation(value = "阅读订单消息")
    @ResponseStatus(HttpStatus.OK)
    @PostMapping(value = "/oneordermsg/{uuid}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uuid", value = "订单信息uuid", required = true),
    })
    @Transactional
    public ResponseEntity OneOrderMsg(@PathVariable("uuid") String uuid) {
        initBaseInfo();
        OrderMsg orderMsg = orderMsgRepository.findByUuid(uuid);
        if(orderMsg.getStatus()==0){
            orderMsg.setStatus(1);
        }
        return ResponseEntity.ok(ApiBaseResponse.fromHttpStatus(HttpStatus.OK,orderMsg));
    }


    @ApiOperation(value = "查询我的钱包消息")
    @ResponseStatus(HttpStatus.OK)
    @PostMapping(value = "/accountmsg")
    @Transactional
    public ResponseEntity getAllaccountmsg() {
        initBaseInfo();

        couponRespository.findByUserId(userId);
        user.getCoupons();
        return ResponseEntity.ok(ApiBaseResponse.fromHttpStatus(HttpStatus.OK));
    }


    @ApiOperation(value = "所有箱子消息")
    @ResponseStatus(HttpStatus.OK)
    @PostMapping(value = "/productmsg")
    @Transactional
    public ResponseEntity getAllProductMsg() {
        initBaseInfo();
        OrderMain orderMain = orderMainRepopsitory.findAllByUserId(userId);
        //Set<ProductMsg> productMsgs = orderMain.getProductMsgs();
        return ResponseEntity.ok(ApiBaseResponse.fromHttpStatus(HttpStatus.OK,orderMain));
    }


    @ApiOperation(value = "阅读箱子消息")
    @ResponseStatus(HttpStatus.OK)
    @PostMapping(value = "/oneproductmsg/{uuid}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uuid", value = "订单信息uuid", required = true),
    })
    @Transactional
    public ResponseEntity OneProductMsg(@PathVariable("uuid") String uuid) {
        initBaseInfo();
        ProductMsg productMsg = productMsgRepository.findByUuid(uuid);
        if(productMsg.getStatus()==0){
            productMsg.setStatus(1);
        }
        return ResponseEntity.ok(ApiBaseResponse.fromHttpStatus(HttpStatus.OK,productMsg));
    }



}


